from rest_framework import status
from rest_framework.generics import GenericAPIView, ListAPIView, ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from meiduo_admin.serializers.users import AdminAuthSerializer, UserSerializer
from users.models import User


class AdminAuthorizeView(APIView):
    def post(self,request):
        serializer = AdminAuthSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        serializer.save()

        return Response(serializer.data,status=status.HTTP_201_CREATED)


# =========================查询用户==================
# class UserInfoView(APIView):
#     permission_classes = [IsAdminUser]
#
#     def get(self,request):
#         """
#         获取普通用户数据:
#         1. 获取keyword关键字
#         2. 查询普通用户数据
#         3. 将用户数据序列化并返回
#         """
#         keyword = request.query_params.get('kwyword')
#
#         if keyword:
#             users = User.objects.filter(is_staff=False, username__contains=keyword)
#         else:
#             users = User.objects.filter(is_staff=False)
#         serializer = UserSerializer(users, many=True)
#         return Response(serializer.data)
#
#
# class UserInfoView(GenericAPIView):
#     permission_classes = [IsAdminUser]
#
#     serializer_class = UserSerializer
#
#     def get_queryset(self):
#         keyword = self.request.query_params.get('password')
#
#         if keyword:
#             users = User.objects.filter(is_staff=False, username_contains=keyword)
#         else:
#             users = User.objects.filter(is_staff=False)
#
#         return users
#
#     def get(self,request):
#         users = self.get_queryset()
#         serializer = self.get_serializer(users, many=True)
#         return Response(serializer.data)


class UserInfoView(ListAPIView):
    permission_classes = [IsAdminUser]
    serializer_class = UserSerializer

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword:
            users = User.objects.filter(is_staff=False, username__contains=keyword)
        else:
            users = User.objects.filter(is_staff=False)

        return users


